/*
 *  Copyright 2020-2021 Li Fei
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.pengshun.modules.api.coal.controller;

import com.pengshun.modules.api.coal.para.CoalBinessPara;
import com.pengshun.modules.api.coal.para.MapPara;
import com.pengshun.modules.api.coal.para.MerchantPara;
import com.pengshun.modules.api.coal.service.MerchantService;
import com.pengshun.modules.api.coal.validator.First;
import com.pengshun.modules.api.coal.validator.Fourth;
import com.pengshun.modules.api.coal.validator.Second;
import com.pengshun.modules.api.coal.validator.Third;
import com.pengshun.modules.common.BaseResult;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author lifei
 * @date 2020-11-05
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "商户管理")
@RequestMapping("/api/merchant")
@ApiIgnore
public class MerchantController {

    private final MerchantService merchantService;


    @ApiOperation(value = "根据类别查询商户信息列表", notes = "商户类别id 1煤炭贸易公司 2运输公司")
    @GetMapping(value = "/info")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantCategoryId", value = "商户类别id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "lngX", value = "经度", dataType = "double", paramType = "query"),
            @ApiImplicitParam(name = "latY", value = "纬度", dataType = "double", paramType = "query")
    })

    public BaseResult selectListByMerchantId(@ApiIgnore @Validated(value = {Second.class}) MerchantPara para, BindingResult paraError) throws Exception {
        if (paraError.hasErrors()) {
            return new BaseResult(1, paraError.getFieldError().getDefaultMessage());
        }
        try {
            BaseResult res = merchantService.selectListByMerchantId(para);
            return res;
        } catch (Exception e) {
            return new BaseResult(1, "查询商户信息列表失败");
        }
    }

    @ApiOperation(value = "根据矿口id查询商户信息列表", notes = " ")
    @GetMapping(value = "/info/mineMouthId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mineMouthId", value = "矿口id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "merchantCategoryId", value = "商户类别id 1煤炭贸易公司 2运输公司 ", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "条数", dataType = "int", required = true, paramType = "query"),
    })
    public BaseResult selectListByMineMouthId(@ApiIgnore @Validated(value = {Fourth.class}) MerchantPara para, BindingResult paraError) throws Exception {
        if (paraError.hasErrors()) {
            return new BaseResult(1, paraError.getFieldError().getDefaultMessage());
        }
        try {
            BaseResult res = merchantService.selectListByMineMouthId(para);
            return res;
        } catch (Exception e) {
            return new BaseResult(1, "查询商户信息列表失败");
        }
    }

    @ApiOperation(value = "条件查询商户信息列表", notes = " ")
    @GetMapping(value = "/info/list")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "category", value = "分类  大块传big_coal 中快medium_coal 小块small_coal ", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "商户名称 ", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "默认不传 0营业中,1暂停营业", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "默认不传 1煤炭数排序 2营业状态排序 ", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "provinceId", value = "默认不传 省份id ", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "cityId", value = "默认不传 城市id ", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "coalCategoryId", value = "煤种id ", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "districtId", value = "默认不传 区县编码id ", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "merchantCategoryId", value = "商户类别id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "mineMouthId", value = "矿口id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "条数", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "level", value = "1优质2 推荐", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "lngX", value = "经度", dataType = "double", paramType = "query"),
            @ApiImplicitParam(name = "latY", value = "纬度", dataType = "double", paramType = "query")
    })
    public BaseResult selectListBy(@ApiIgnore @Validated(value = {First.class}) CoalBinessPara para, BindingResult paraError) throws Exception {
        if (paraError.hasErrors()) {
            return new BaseResult(1, paraError.getFieldError().getDefaultMessage());
        }
        try {
            BaseResult res = merchantService.selectListBy(para);
            return res;
        } catch (Exception e) {
            return new BaseResult(1, "查询商户信息列表失败");
        }
    }


    @ApiOperation(value = "信息部详情", notes = " ")
    @GetMapping(value = "/info/detil")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantId", value = "商户id", required = true, dataType = "int", paramType = "query"),
    })
    public BaseResult selectMerchantDetil(@ApiIgnore @Validated(value = {Third.class}) MerchantPara para, BindingResult paraError) throws Exception {
        if (paraError.hasErrors()) {
            return new BaseResult(1, paraError.getFieldError().getDefaultMessage());
        }
        try {
            BaseResult res = merchantService.selectMerchantDetil(para.getMerchantId());
            return res;
        } catch (Exception e) {
            return new BaseResult(1, "查询信息部详情失败");
        }
    }

    @ApiOperation(value = "查找商户/经销商/货运公司地图导航", notes = " ")
    @GetMapping(value = "/info/map")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "longitude", value = "经度", dataType = "double", required = true, paramType = "query"),
            @ApiImplicitParam(name = "latitude", value = "纬度", dataType = "double", required = true, paramType = "query"),
            @ApiImplicitParam(name = "merchantCategoryId", value = "类别id 1煤炭贸易公司 2运输公司 3煤炭经纪人 4矿口直营 5运输公司1 6物流公司 7煤炭经销商 (目前用7) ", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称 ", required = true, dataType = "int", paramType = "query"),
    })
    public BaseResult selectAllMerchantMap(@ApiIgnore @Validated(value = {First.class}) MapPara para, BindingResult paraError) throws Exception {
        if (paraError.hasErrors()) {
            return new BaseResult(1, paraError.getFieldError().getDefaultMessage());
        }
        try {
            BaseResult res = merchantService.selectAllMerchantMap(para);
            return res;
        } catch (Exception e) {
            return new BaseResult(1, "查询商户信息列表失败");
        }
    }
    @ApiOperation(value = "手机号查询商户", notes = " ")
    @GetMapping(value = "/by-phone")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号 ", required = true, dataType = "int", paramType = "query"),
    })
    public BaseResult selectMerchant(@ApiIgnore @Validated(value = {Third.class}) MapPara para, BindingResult paraError) throws Exception {
            BaseResult res = merchantService.selectMerchant(para.getPhone());
            return res;
    }

}